package com.zyb.educenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyb.commonutils.R;
import com.zyb.educenter.entity.UcenterMember;
import com.zyb.educenter.entity.vo.UserQuery;
import com.zyb.educenter.service.UcenterMemberService;
import com.zyb.educenter.utils.MD5;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

//@CrossOrigin
@RestController
@RequestMapping("/educenter/userAdmin")
public class UserAdminController {

    @Resource
    private UcenterMemberService memberService;

    //查询用户信息带分页
    @GetMapping("getUserInfoPage/{page}/{limit}")
    public R getUserInfoPage(@PathVariable Long page, @PathVariable Long limit) {
        Page<UcenterMember> userPage = new Page<>(page, limit);
        memberService.page(userPage, null);
        List<UcenterMember> records = userPage.getRecords();
        long total = userPage.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    //条件查询带分页
    @PostMapping("pageUserCondition/{page}/{limit}")
    public R pageUserCondition(@PathVariable Long page, @PathVariable Long limit,
                               @RequestBody(required = false) UserQuery userQuery) {
        Page<UcenterMember> memberPage = new Page<>(page, limit);
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        //接收前端传过来的条件
        String mobile = userQuery.getMobile();
        String nickName = userQuery.getNickname();
        String begin = userQuery.getBegin();
        String end = userQuery.getEnd();
        if (!StringUtils.isEmpty(mobile)) {
            wrapper.like("mobile", mobile);
        }
        if (!StringUtils.isEmpty(nickName)) {
            wrapper.like("nickname", nickName);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_modified", end);
        }
        //进行排序
        wrapper.orderByDesc("gmt_create");
        memberService.page(memberPage, wrapper);
        List<UcenterMember> records = memberPage.getRecords();
        long total = memberPage.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    //管理员添加用户
    @PostMapping("addUserInfo")
    public R addUserInfo(@RequestBody UcenterMember ucenterMember) {
        boolean flag = memberService.save(ucenterMember);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    //根据用户ID查询一条用户信息
    @GetMapping("getUserInfoById/{userId}")
    public R getUserInfoById(@PathVariable String userId) {
        UcenterMember userInfo = memberService.getById(userId);
        return R.ok().data("userInfo", userInfo);
    }

    //修改用户信息
    @PostMapping("updateUserInfo")
    public R updateUserInfo(@RequestBody UcenterMember ucenterMember) {
        boolean flag = memberService.updateById(ucenterMember);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //删除用户
    @DeleteMapping("deleteUser/{userId}")
    public R deleteUser(@PathVariable String userId) {
        boolean flag = memberService.removeById(userId);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
